const domainName = getApp().globalData.domainName
const oss_domain = getApp().globalData.oss_domain
const parameter = getApp().globalData.parameter
// pages/mana_jg/mana_jg.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jg_list:[],
  },

  search_text(e){
    let search_text = e.detail.value
    this.setData({
      search_text:search_text
    })
  },

  go_search(){
    this.load_jg_list(0,this.data.search_text)
  }, 

  cancel_search(){
    this.setData({
      search_text:''
    })
  },

  // 查看机构
  view_jg(e){
    let index = e.currentTarget.id
    this.setData({
      sel_index:index,
    })
    let jg_id = this.data.jg_list[index].jg_id
    let short_name = this.data.jg_list[index].short_name
    wx.showActionSheet({
      itemList: ['机构主页','学生情况','变更负责人','设置机构失效日期'],
      success: res => {
        if(res.tapIndex==0){
          wx.navigateTo({
            url:'/pages/jgs/jg_homePage/jg_homePage?jg_id=' + jg_id
          })
        }else if(res.tapIndex==1){
          wx.navigateTo({
            url: '/pages/students/index/index?jg_id=' + jg_id + '&jg_name=' + short_name,
            fail:err=>{
              console.log('err',err)
            }
          })
        }else if(res.tapIndex==2){
          wx.navigateTo({
            url: '/pages/jgs/jg_header/jg_header?jg_id=' + jg_id,
          })
        }else if(res.tapIndex==3){
          wx.navigateTo({
            url:'/pages/jgs/jg_permission/jg_permission?jg_id=' + jg_id
          })
        }
      },
    })

  },


  // 变更机构负责人
  update_jg_header(jg_id, phone){
    wx.request({
      url: domainName + '/update_jg_header',
      data: {
        jg_id:jg_id,
        phone:phone,
      },...parameter,
      success: res => {
        if(res.data.update_res){
          wx.showToast({
            title: '更变成功！',
            duration: 1500,
            icon: 'success',
          })
          this.onShow()
        }
      },
    })
  },




  // 加载机构列表
  load_jg_list(page_no,search_text){
    wx.request({
      url: domainName + '/search_jg_list',
      data: {
        page_no,
        search_text,
      },...parameter,
      success: res => {
        let jg_list = res.data.jg_list
        console.log('jg_list',res.data)

        if(page_no>0){
          this.setData({
            jg_list:this.data.jg_list.concat(jg_list),
          })
          this.page_no += 1

        }else{
          this.setData({
            jg_list:jg_list,
          })
          this.page_no = 1
        }
        if(jg_list.length==0){
          this.setData({
            no_more:true
          })
        }
        wx.stopPullDownRefresh()
      },
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.no_more){
      this.load_jg_list(this.page_no,this.data.search_text)
    }
  },

  onPullDownRefresh(){
    this.load_jg_list(0,this.data.search_text)
  }

})